package com.mzba.easy.weibo.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.mzba.easy.weibo.util.StringUtil;

/**
 * @author 06peng
 *
 */
public class CommentsTimelineTable {

	public String TABLE_NAME = "comment_db";
	public static final String _id = "_id";
	public static final String created_at = "created_at";
	public static final String id = "id";
	public static final String text = "text";
	public static final String source = "source";
	public static final String mid = "mid";
	public static final String user_id = "user_id";
	public static final String status_text = "status_text";
	public static final String status_user_id = "status_user_id";
	public static final String status_user_screen_name = "status_user_screen_name";
	public static final String reply_comment = "reply_comment";
	public static final String screen_name = "screen_name";
	public static final String profile_image_url = "profile_image_url";
	/**评论类型 all：全部评论；tome：我收到的评论；byme：我发出的评论；mentionme：@我的评论*/
	public static final String comment_type = "comment_type";

	
	private DBManagerImpl db = null;
	
	public CommentsTimelineTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	public void createTable() {
		String createTableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(_id integer primary key autoincrement, " 
				+ created_at + "  VARCHAR," 
				+ id + " VARCHAR,"
				+ text + " VARCHAR,"
				+ source + " VARCHAR," 
				+ mid + " VARCHAR,"
				+ status_text + " VARCHAR," 
				+ reply_comment + " VARCHAR,"
				+ user_id + " VARCHAR,"
				+ screen_name + " VARCHAR,"
				+ comment_type + " VARCHAR,"
				+ status_user_id + " VARCHAR,"
				+ status_user_screen_name + " VARCHAR,"
				+ profile_image_url + " VARCHAR)";
		db.creatTable(db.getConnection(), createTableSql, TABLE_NAME);
		
	}
	
	public void save(List<CommentEntry> entries, String commentType) {
		List<ContentValues> arraylist = new ArrayList<ContentValues>();
		db.delete(db.getConnection(), TABLE_NAME, " where comment_type = ? ", new String[]{ commentType });
		for (int i = 0;i < 20 && i < entries.size();i++) {
			CommentEntry entry = entries.get(i);
			ContentValues values = new ContentValues();
			values.put(created_at, entry.getCreated_at());
			values.put(id, entry.getId());
			values.put(text, entry.getText());
			values.put(source, entry.getSource());
			values.put(status_text, entry.getStatus() == null ? "" : entry.getStatus().getText());
			values.put(reply_comment, entry.getReply_comment() == null ? "" : entry.getReply_comment().getText());
			values.put(mid, entry.getMid());
			values.put(user_id, entry.getUser().getId());
			values.put(screen_name, entry.getUser().getScreen_name());
			values.put(profile_image_url, entry.getUser().getProfile_image_url());
			values.put(comment_type, commentType);
			values.put(status_user_id, entry.getStatus().getUser() == null ? "" : entry.getStatus().getUser().getId());
			values.put(status_user_screen_name, entry.getStatus().getUser() == null ? "" : entry.getStatus().getUser().getScreen_name());
			arraylist.add(values);
		}
		db.saveAll(db.getConnection(), TABLE_NAME, arraylist);
	}
	
	
	public List<CommentEntry> getAllComments(String commentType) {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME + " where comment_type = ?", new String[]{ commentType });
		if (cursor == null)
			return null;
		List<CommentEntry> entries = new ArrayList<CommentEntry>();
		try {
			while (cursor.moveToNext()) {
				CommentEntry entry = new CommentEntry();
				entry.setCreated_at(cursor.getString(cursor.getColumnIndex(created_at)));
				entry.setId(cursor.getString(cursor.getColumnIndex(id)));
				entry.setText(cursor.getString(cursor.getColumnIndex(text)));
				entry.setSource(cursor.getString(cursor.getColumnIndex(source)));
				entry.setMid(cursor.getString(cursor.getColumnIndex(mid)));
				if (StringUtil.isNotBlank(cursor.getString(cursor.getColumnIndex(reply_comment)))) {
					CommentEntry comment = new CommentEntry();
					comment.setText(cursor.getString(cursor.getColumnIndex(reply_comment)));
					entry.setReply_comment(comment);
				}
				if (StringUtil.isNotBlank(cursor.getString(cursor.getColumnIndex(status_text)))) {
					StatusTimelineEntry status = new StatusTimelineEntry();
					status.setText(cursor.getString(cursor.getColumnIndex(status_text)));
					if (StringUtil.isNotBlank(cursor.getString(cursor.getColumnIndex(status_user_screen_name)))) {
						UserEntry user = new UserEntry();
						user.setId(cursor.getString(cursor.getColumnIndex(status_user_id)));
						user.setScreen_name(cursor.getString(cursor.getColumnIndex(status_user_screen_name)));
						status.setUser(user);
					}
					entry.setStatus(status);
				}
				UserEntry user = new UserEntry();
				user.setId(cursor.getString(cursor.getColumnIndex(user_id)));
				user.setScreen_name(cursor.getString(cursor.getColumnIndex(screen_name)));
				user.setProfile_image_url(cursor.getString(cursor.getColumnIndex(profile_image_url)));
				entry.setUser(user);
				entries.add(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entries;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
	}
}
